<template>
  <main class="w-full h-full dark:bg-black p-4">
    <use-table
      :columns="columns"
      :search-select-option="searchSelectOption"
      :all-data="allData"
      v-model:selected-row-key="selectedRowKey"
      :personalized="true"
      :loading="loading"
      :total="total"
      @delete-select-user="deleteSelects"
      v-model:visible="modifySelectFormVisible"
      @modifyUserData="openModifyRadioForm"
    >
      <template #title> 多选题 </template>
      <template #function>
        <a-tooltip placement="bottom">
          <template #title>
            <span>添加题目</span>
          </template>
          <div class="cursor-pointer text-lg text-blue-500 mr-2" @click="openAddSelectForm">
            <plus-circle-outlined />
          </div>
        </a-tooltip>
        <a-tooltip placement="bottom">
          <template #title>
            <span>删除选中题目</span>
          </template>
          <div class="cursor-pointer text-lg text-red-500" @click="deleteSelects">
            <close-circle-outlined />
          </div>
        </a-tooltip>
      </template>
    </use-table>
    <modify-form
      v-model:visible="addSelectFormVisible"
      modal-title-text="添加多选题"
      @handleOk="completeAddSelectForm"
    >
      <template #container>
        <div class="flex items-center">
          <span class="w-22 text-right"> 题目名称： </span>
          <a-input v-model:value="addSelectFormData.selection_name" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 选项A： </span>
          <a-input v-model:value="addSelectFormData.A" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 选项B： </span>
          <a-input v-model:value="addSelectFormData.B" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 选项C： </span>
          <a-input v-model:value="addSelectFormData.C" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 选项D： </span>
          <a-input v-model:value="addSelectFormData.D" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 正确答案： </span>
          <a-checkbox-group
            v-model:value="(addSelectFormData.right_key as string[])"
            name="checkboxgroup"
            :options="plainOptions"
          />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 所属科目： </span>
          <a-select v-model:value="addSelectFormData.subject_name" style="width: 180px">
            <a-select-option v-for="i in subjectList" :value="i.subject_name">
              {{ i.subject_name }}
            </a-select-option>
          </a-select>
        </div>
      </template>
    </modify-form>
    <modify-form
      v-model:visible="modifySelectFormVisible"
      modal-title-text="修改多选题信息"
      @handleOk="completeModifyRadioForm"
    >
      <template #container>
        <div class="flex items-center">
          <span class="w-22 text-right"> 题目名称： </span>
          <a-input v-model:value="modifySelectForm.selection_name" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 选项A： </span>
          <a-input v-model:value="modifySelectForm.A" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 选项B： </span>
          <a-input v-model:value="modifySelectForm.B" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 选项C： </span>
          <a-input v-model:value="modifySelectForm.C" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 选项D： </span>
          <a-input v-model:value="modifySelectForm.D" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 正确答案： </span>
          <a-checkbox-group
            v-model:value="(modifySelectForm.right_key as string[])"
            name="checkboxgroup"
            :options="plainOptions"
          />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-22 text-right"> 所属科目： </span>
          <a-select v-model:value="modifySelectForm.subject_name" style="width: 180px">
            <a-select-option v-for="i in subjectList" :value="i.subject_name">
              {{ i.subject_name }}
            </a-select-option>
          </a-select>
        </div>
      </template>
    </modify-form>
  </main>
</template>

<script setup lang="ts">
  import { getAllSelect, deleteSelect, addSelect, updateSelect } from '@/api/examination';
  import { getSubject } from '@/api/student';
  import { AddSelectData, SubJect, SelectData } from '@/utils/interface';
  import { CloseCircleOutlined, PlusCircleOutlined } from '@ant-design/icons-vue';
  import { message } from 'ant-design-vue';

  let allData = ref<SelectData[]>([]);
  let loading = ref(false);
  let total = ref(0);
  // 勾选的数据的key
  let selectedRowKey = ref<number[] | string[]>([]);
  // 搜索框下拉列表
  const searchSelectOption: { value: string; label: string }[] = [
    {
      value: 'major_name',
      label: '专业名称',
    },
    {
      value: 'subject_name',
      label: '科目名称',
    },
    {
      value: 'selection_name',
      label: '题目名称',
    },
  ];
  const columns = [
    {
      title: '题目名称',
      dataIndex: 'selection_name',
      align: 'center',
      width: 140,
      ellipsis: true,
    },
    {
      title: '所属科目',
      dataIndex: 'subject_name',
      align: 'center',
      width: 140,
      ellipsis: true,
    },
    {
      title: '所属专业',
      dataIndex: 'major_name',
      align: 'center',
      width: 140,
      ellipsis: true,
    },
    {
      title: '答案A',
      dataIndex: 'A',
      align: 'center',
      ellipsis: true,
    },
    {
      title: '答案B',
      dataIndex: 'B',
      align: 'center',
      ellipsis: true,
    },
    {
      title: '答案C',
      dataIndex: 'C',
      align: 'center',
      ellipsis: true,
    },
    {
      title: '答案D',
      dataIndex: 'D',
      align: 'center',
      ellipsis: true,
    },
    {
      title: '正确答案',
      dataIndex: 'show_key',
      align: 'center',
      width: 80,
    },
    {
      title: '操作',
      dataIndex: 'operation',
      align: 'center',
      width: 80,
    },
  ];
  const getSelectList = async () => {
    loading.value = true;
    const { data } = await getAllSelect();
    allData.value = data.filter((item) => item.status !== 0);
    allData.value.forEach((item) => (item.show_key = (item.right_key as any).join(',')));
    allData.value.forEach((item) => (item.right_key = item.right_key));
    total.value = data.length;
    loading.value = false;
  };

  let subjectList = ref<SubJect[]>([]);
  getSelectList();
  getSubject().then((res) => {
    subjectList.value = res.data as SubJect[];
  });
  const deleteSelects = async (key: number[] | string[] | any) => {
    const selectKey = Array.isArray(key) ? key : selectedRowKey.value;
    if (selectKey.length === 0) {
      message.warning('请选择要删除的题目');
      return;
    }
    const res = await deleteSelect(selectKey);
    if (res.status === 200) {
      message.success('删除成功');
      getSelectList();
    }
  };

  const plainOptions = ['A', 'B', 'C', 'D'];
  let addSelectFormVisible = ref(false);
  let addSelectFormData = ref<AddSelectData>({
    selection_name: '',
    A: '',
    B: '',
    C: '',
    D: '',
    right_key: [],
    subject_name: '',
  });
  const openAddSelectForm = () => {
    addSelectFormVisible.value = true;
  };
  const completeAddSelectForm = async () => {
    const { selection_name, right_key, subject_name, A, B, C, D } = addSelectFormData.value;

    if (!selection_name || !right_key || !subject_name || !A || !B || !C || !D) {
      message.warning('请填写完整信息');
      return;
    }
    let lastKey = right_key;
    const { status, msg } = (await addSelect({
      selection_name,
      right_key: lastKey,
      subject_name,
      A,
      B,
      C,
      D,
    })) as any;
    if (status === 200) {
      message.success(msg);
      addSelectFormData.value = {
        selection_name: '',
        A: '',
        B: '',
        C: '',
        D: '',
        right_key: [],
        subject_name: '',
      };
      addSelectFormVisible.value = false;
      await getSelectList();
    }
  };

  let modifySelectFormVisible = ref(false);
  let modifySelectForm = ref({
    selection_name: '',
    A: '',
    B: '',
    C: '',
    D: '',
    right_key: [],
    subject_name: '',
    key: 0,
  });
  const openModifyRadioForm = (key: number) => {
    const clickUser = allData.value.find((item: any) => item.key === key) as any;
    console.log(clickUser);

    if (clickUser !== undefined) {
      modifySelectForm.value = {
        ...clickUser,
      };
      modifySelectFormVisible.value = true;
    } else message.error('发生错误');
  };
  const completeModifyRadioForm = async () => {
    const { selection_name, right_key, subject_name, A, B, C, D, key } = modifySelectForm.value;
    if (!selection_name || !right_key || !subject_name || !A || !B || !C || !D) {
      message.warning('请填写完整信息');
      return;
    }
    const { status, msg } = (await updateSelect({
      id: key as number,
      selection_name,
      right_key: right_key as any,
      subject_name,
      A,
      B,
      C,
      D,
    })) as any;
    if (status === 200) {
      message.success(msg);
      await getSelectList();
      modifySelectFormVisible.value = false;
    }
  };
</script>

<style scoped>
  :deep(.ant-radio-group-default) {
    @apply flex;
  }
</style>
